/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Entity.Dosen;
import Entity.Mahasiswa;
import Entity.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import library.Koneksi;


/**
 *
 * @author mr2
 */
public class MahasiswaModel {
    
        
    Connection koneksi;
    Dosen dosen;
    Mahasiswa mahasiswa;
    User user;
    
    public MahasiswaModel(){
        koneksi = Koneksi.getKoneksi();
    }
    
    public List<Mahasiswa> ListMahasiswa(){
        
        PreparedStatement prepare = null;
        ResultSet result = null;
        List<Mahasiswa> listMahasiswa = new ArrayList<Mahasiswa>();
        
        try{
            String sql = "Select mahasiswa.id_user, user.nama_user, smt_mhs, dosen_wali, tahun_masuk_mhs, status_transfer, kelulusan, status_keaktifan "
                    + "from mahasiswa, user where user.id_user = mahasiswa.id_user ";
            
            prepare = koneksi.prepareStatement(sql);
            
            result = prepare.executeQuery();
            
            while(result.next()){
                Mahasiswa mahasiswa = new Mahasiswa();
                User user = new User();
                user.setIdUser(result.getString("id_user"));
                user.setNamaUser(result.getString("nama_user"));
                mahasiswa.setUser(user);
                mahasiswa.setSmtMhs(result.getInt("smt_mhs"));
                mahasiswa.setDosenWali(result.getString("dosen_wali"));
                mahasiswa.setThnMasuk(result.getString("tahun_masuk_mhs"));
                mahasiswa.setStatusTransfer(result.getString("status_transfer"));
                mahasiswa.setKelulusan(result.getInt("kelulusan"));
                mahasiswa.setStatusAktif(result.getInt("status_keaktifan"));
                
                listMahasiswa.add(mahasiswa);
            }
            return listMahasiswa;
            
        }catch(SQLException ex){
            System.out.print(ex.getMessage());
            return listMahasiswa;            
        }finally{
            if(prepare != null){
                try{
                    prepare.close();
                }catch(SQLException ex){
                    
                }
            }
            if(result != null){
                try{
                    result.close();
                }catch(SQLException ex){
                    
                }
            }
        }
    }
    
    public Mahasiswa DetailMahasiswa(String idUser){
        
        PreparedStatement prepare = null;
        ResultSet result = null;
        
        try{
            String sql = "Select * from Mahasiswa, user where mahasiswa.id_user = user.id_user and user.id_user = '"+idUser+"'";
            
            prepare = koneksi.prepareStatement(sql);
            
            result = prepare.executeQuery();
            
            while(result.next()){
                this.mahasiswa = new Mahasiswa();
                User user = new User();
                user.setIdUser(result.getString("id_user"));
                user.setNamaUser(result.getString("nama_user"));
                this.mahasiswa.setUser(user);
                this.mahasiswa.setSmtMhs(result.getInt("smt_mhs"));
                this.mahasiswa.setDosenWali(result.getString("dosen_wali"));
                //mahasiswa.setThnMasuk(result.getString("tahun_masuk_mhs"));
                this.mahasiswa.setStatusTransfer(result.getString("status_transfer"));
                this.mahasiswa.setKelulusan(result.getInt("kelulusan"));
                this.mahasiswa.setStatusAktif(result.getInt("status_keaktifan"));
            }
            
            
        }catch(SQLException ex){
            System.out.print(ex.getMessage());            
        }finally{
            if(prepare != null){
                try{
                    prepare.close();
                }catch(SQLException ex){
                    
                }
            }
            if(result != null){
                try{
                    result.close();
                }catch(SQLException ex){
                    
                }
            }
        }
        
        return this.mahasiswa;
    }
    
}